﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using POS.Framework;
using BLL;
using Model.Bill;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using Model;
using BLL.Enums;

namespace POS.ModForm.Bill
{
    public partial class frmAdjustPriceForm : DevExpress.XtraEditors.XtraForm
    {
        ProductBLL productBLL = new ProductBLL();
        TB_AdjustPrice adjustPrice = null;
        MyGridChedkMarksSelectionHelper ChedkSelect { get; set; }
        public frmAdjustPriceForm()
        {
            InitializeComponent();
        }
        private void frmAdjustPriceForm_Load(object sender, EventArgs e)
        {
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                this.gridView.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(SpecialGridView.DrawRowIndicator);
                this.ChedkSelect = new MyGridChedkMarksSelectionHelper(this.gridView);
                this.SetButtonClickEvent();
                this.GetModule();
                wdf.Close();
            }
        }
        void SetButtonClickEvent()
        {
            this.formButtonUC.IsHide(new List<string> { "btnCostSet" });
            this.formButtonUC.Save.Click += Save_Click;
            this.formButtonUC.Examine.Click += Examine_Click;
            this.addItemAdjustPrice.AddDetail.Click += AddDetail;
            this.addItemAdjustPrice.DelDetail.Click += DelDetail;
            this.addItemAdjustPrice.SalePriceLowestControl.KeyPress += SalePriceLowestControl_KeyPress;
            this.addItemAdjustPrice.LookUpProduct.Properties.DataSource = productBLL.GetProducts();
            this.addItemAdjustPrice.LookUpProduct.Select();
        }
        void Save_Click(object sender, EventArgs e)
        {
            if (adjustPrice != null && adjustPrice.StatusID != (int)AdjustPriceStatus.Editor) 
            {
                MsgBox.ShowTips("调价单已审核不能进行保存基本信息！");
                return;
            }
            Session s=DatabaseSchema.Session;
            adjustPrice = new TB_AdjustPrice(s);
            adjustPrice.AdjustPriceID = PlanIDBLL.GetPlanID(s,"TJ");
            adjustPrice.BillCode = adjustPrice.AdjustPriceID;
            adjustPrice = new TB_AdjustPrice(DatabaseSchema.Session);
            adjustPrice.AdjustPriceID = Guid.NewGuid().ToString();
            adjustPrice.BillCode = Guid.NewGuid().ToString();
            adjustPrice.StatusID = (int)AdjustPriceStatus.Editor;
            adjustPrice.StoreID = DatabaseSchema.Session.GetObjectByKey<B_Store>(MemberInfo.Store.StoreID);
            adjustPrice.Title = this._Title.Text.Trim();
            adjustPrice.Reason = this._Reason.Text.Trim();
            adjustPrice.Save();
            MsgBox.ShowTips("保存调价单的基本信息成功！");
            ShowOrderUI();
        }
        void Examine_Click(object sender, EventArgs e)
        {
            if (adjustPrice == null)
            {
                MsgBox.ShowTips("请先保存调价单的基本信息！");
                return;
            }
            adjustPrice.AdjustPriceDetails.Reload();
            if (adjustPrice.AdjustPriceDetails.Count==0)
            {
                MsgBox.ShowTips("请添加要调价的商品明细！");
                return;
            }
            Session s = DatabaseSchema.Session;
            try
            {
                s.BeginTransaction();
                adjustPrice.StatusID = (int)AdjustPriceStatus.Complete;
                adjustPrice.AuditUser = MemberInfo.User.LoginName;
                adjustPrice.AuditDate = DateTime.Now;
                adjustPrice.Save();
                foreach (TB_AdjustPriceDetail apd in adjustPrice.AdjustPriceDetails)
                {
                    apd.ProductID.SalePrice = apd.NewSellPrice;
                    apd.ProductID.SalePriceLowest = apd.NewLowestSellPrice;
                    apd.ProductID.Save();
                }
                s.CommitTransaction();
                MsgBox.ShowTips("审核成功！");
                ShowOrderUI();
            }
            catch (Exception ex)
            {
                s.RollbackTransaction();
                MsgBox.ShowTips(ex.Message);
            }
        }
        void AddDetail(object sender, EventArgs e)
        {
            if (adjustPrice == null)
            {
                MsgBox.ShowTips("请先保存调价单的基本信息！");
                return;
            }
            Session s = DatabaseSchema.Session;
            TB_AdjustPriceDetail apd = s.FindObject<TB_AdjustPriceDetail>(
                CriteriaOperator.Parse("AdjustPriceID=? And ProductID=?", adjustPrice.AdjustPriceID, addItemAdjustPrice.ProductID));
            if (apd != null)
            {
                MsgBox.ShowTips("选择要调价的货品已存在调价明细中！");
                return;
            }
            apd = new TB_AdjustPriceDetail(s);
            apd.AdjustPriceID = adjustPrice;
            apd.ProductID = s.GetObjectByKey<P_Product>(addItemAdjustPrice.ProductID);
            apd.OldSellPrice = apd.ProductID.SalePrice;
            apd.NewSellPrice = addItemAdjustPrice.SalePrice;
            apd.OldLowestSellPrice = apd.ProductID.SalePriceLowest;
            apd.NewLowestSellPrice = addItemAdjustPrice.SalePriceLowest;
            apd.Save();
            MsgBox.ShowTips("保存调价单的明细货品成功！");
            adjustPrice.AdjustPriceDetails.Reload();
            this.gridControl.DataSource = adjustPrice.AdjustPriceDetails;
        }
        void DelDetail(object sender, EventArgs e)
        {
            if (this.ChedkSelect.SelectedCount <= 0)
            {
                MsgBox.ShowWarning("请选中要删除的调价货品!");
                return;
            }
            try
            {
                foreach (TB_AdjustPriceDetail od in this.ChedkSelect._Selection)
                {
                    if (od != null)
                        od.Delete();
                }
                MsgBox.ShowTips("删除调价单明细成功！");
            }
            catch (Exception ex)
            {
                MsgBox.ShowWarning(ex.Message);
                return;
            }
            adjustPrice.AdjustPriceDetails.Reload();
            this.gridControl.DataSource = adjustPrice.AdjustPriceDetails;
        }
        void SalePriceLowestControl_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
                AddDetail(sender, e);
        }
        void ShowOrderUI()
        {
            if (adjustPrice != null)
            {
                this.groupControl.Text = string.Format("调价单号：{0}  状态：{1}", adjustPrice.BillCode, EnumManage.GetBillStatus(typeof(OrderStatus), adjustPrice.StatusID));
                if (adjustPrice.StatusID == (int)AdjustPriceStatus.Complete)
                {
                    this.addItemAdjustPrice.Enabled = false;
                }
            }
        }
        void GetModule()
        {
            if (this.Tag != null && !string.IsNullOrEmpty(this.Tag.ToString()))
            {
                adjustPrice = DatabaseSchema.Session.GetObjectByKey<TB_AdjustPrice>(this.Tag.ToString());
                this.gridControl.DataSource = adjustPrice.AdjustPriceDetails;
                ShowOrderUI();
            }
        }
    }
}